package com.cm.sys.system.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cm.sys.framework.application.ApiResponseCode;
import com.cm.sys.framework.execption.BusinessException;
import com.cm.sys.system.dao.RoleAuthMapper;
import com.cm.sys.system.entity.RoleAuth;
import com.cm.sys.system.service.RoleAuthService;

@Service
public class RoleAuthServiceImpl implements RoleAuthService {

    private static final Logger logger = LoggerFactory.getLogger(RoleAuthService.class);

    @Autowired
    private RoleAuthMapper roleAuthMapper;

    @Override
    public void saveAuth(List<RoleAuth> list) {

        logger.info("角色权限保存对象的入参==================【{}】===============", JSON.toJSONString(list));
        roleAuthMapper.insert(list);
        logger.info("角色权限保存返回的值==================【{}】===============", JSON.toJSONString(list));
        // return roleAuth;

    }

    @Override
    public int delAuth(JSONObject args, Integer roleId) {
        logger.info("角色权限删除的入参==================【{}】===============", args);
        int count = 0;
        count = roleAuthMapper.deleteByPrimaryKey(roleId);
        return count;
    }

    @Override
    public RoleAuth updateAuth(JSONObject args, Integer userId) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<RoleAuth> selectAuths(JSONObject args) {

        logger.info("角色权限列表查询的入参==================【{}】===============", args);

        List<RoleAuth> list = roleAuthMapper.selectAll();

        logger.info("角色权限列表查询的返回值==================【{}】===============", JSON.toJSONString(list));
        return list;
    }

    @Override
    public RoleAuth selectAuth(Integer authId) {
        RoleAuth roleAuth = new RoleAuth();
        if (StringUtils.isEmpty(authId)) {
            throw new BusinessException(ApiResponseCode.PARAM_NEED.getCode(), "所传参数为空");
        } else {
            roleAuth = roleAuthMapper.selectByPrimaryKey(authId);
        }
        return roleAuth;
    }

    @Override
    public List<Integer> getAuths(Integer userRole) {
        List<Integer> list = roleAuthMapper.selectByRoleMenu(userRole);

        return list;
    }

    @Override
    public List<Integer> getUseRole(Integer userRole) {
        List<Integer> list = roleAuthMapper.selectUserByRole(userRole);
        return list;
    }
}
